-
Notifications
You must be signed in to change notification settings - Fork 61
Add database migration CLI tool #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pikaju
wants to merge
78
commits into
main
Choose a base branch
from
diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a complete migration system to Toasty: a CLI that diffs your Rust models against the last known schema snapshot, generates SQL migration files, and applies them to your database. Supports SQLite, PostgreSQL, and MySQL, with database-aware DDL generation (e.g. SQLite's table-recreation pattern for column type changes).
Usage
1. Configure your project with a
Toasty.tomlat the root:2. Wire up the CLI in a binary (
src/bin/cli.rs):3. Generate a migration when your models change:
This diffs your current schema against the last snapshot and produces a SQL migration file. If it detects a potential rename (table, column, or index), it interactively prompts you:
4. Apply pending migrations to your database:
5. Drop a migration (interactive picker or by name):
6. Print the current schema snapshot:
What generated files look like
A generated migration (
toasty/migrations/0000_migration.sql):Each statement is separated by
-- #[toasty::breakpoint]markers so the driver can execute them individually within a transaction.The corresponding snapshot (
toasty/snapshots/0000_snapshot.toml) captures the full schema state in TOML:A
history.tomlfile ties migrations to their snapshots:Implementation details
toasty-core): Rename-aware diff algorithms for tables, columns, and indices with aRenameHintssystem to distinguish renames from drop+createtoasty-sql): New DDL statement types (ALTER TABLE, ALTER COLUMN, ADD/DROP COLUMN, DROP INDEX, COPY TABLE, PRAGMA) with per-database serializationSchemaMutationscapability struct -- SQLite can'tALTER COLUMN(uses table recreation), MySQL supports atomic multi-propertyALTER COLUMN, PostgreSQL requires separate statements per property changegenerate_migration(),applied_migrations(), andapply_migration(). Migrations tracked in a__toasty_migrationstable, applied within transactions. DynamoDB stubbed as unimplemented.Stats
~6,000 lines added across 70 files (new
toasty-clicrate, extensions totoasty-core,toasty-sql, and all SQL drivers)